import { request } from '../../request/index.js'
import Notify from '@vant/weapp/notify/notify';
import Dialog from '@vant/weapp/dialog/dialog';
Page({
  /**
   * 页面的初始数据
   */
  data: {
    userInfo: {},
    hasUserInfo: false,
    bindMobile: '>',
    show: false,
  },
  //弹窗
  onClose() {
    this.setData({
      show: !this.data.show,
      avatarUrl: '',
      nickName: '',
    })
  },
  //获取头像
  getAvatar(e) {
    this.setData({
      avatarUrl: e.detail.avatarUrl
    })
  },
  //获取昵称
  getName(e) {
    this.setData({
      nickName: e.detail.value
    })
  },
  //登录
  login() {
    let that = this;
    let userinfo = {}
    userinfo.avatarUrl = that.data.avatarUrl
    userinfo.nickName = that.data.nickName
    //将用户信息存入缓存
    if (userinfo.nickName) {
      //登录
      wx.login({
        success: res => {
          if (res.code) {
            let data = {
              code: res.code,
              nickName: userinfo.nickName,
              pic: userinfo.avatarUrl ? userinfo.avatarUrl : '',
            }
            request({
              url: '/user/wxLogin',
              method: 'POST',
              data: data,
            }).then(res => {
              if (res.code === 0) {
                if (res.data) {
                  wx.setStorageSync('token', res.data); // 存储token
                  wx.setStorageSync('userinfo', userinfo)
                  //获取当前时间戳，以便后续清除缓存
                  const timestamp = Date.now();
                  wx.setStorageSync('cacheTimestamp', timestamp);
                  that.setData({
                    userInfo: userinfo,
                    hasUserInfo: true,
                    show: false
                  })
                }
              } else {
                wx.showToast({
                  title: '错误',
                  icon: 'error'
                })
              }
            })
          } else {
            console.log('登录失败！' + res.errMsg)
          }
        }
      });
    } else {
      Notify('请输入用户名');
    }
  },
  //退出登录
  logout() {
    let that = this;
    Dialog.confirm({
      title: '退出',
      message: '您确认要退出吗？',
    }).then(() => {
      // on confirm
      wx.removeStorageSync('userinfo');
      wx.removeStorageSync('token');
      wx.removeStorageSync('cacheTimestamp');
      that.setData({
        userInfo: null,
        avatarUrl: '',
        nickName: '',
        hasUserInfo: false,
      })
    }).catch(() => {
      // on cancel
    });
  },
  onShow() {
    const cacheTimestamp = wx.getStorageSync('cacheTimestamp') || 0;
    const twelveHoursInMs = 12 * 60 * 60 * 1000; // 12小时的毫秒数
    const currentTime = Date.now();
    
    if (currentTime - cacheTimestamp > twelveHoursInMs) {
      // 如果缓存时间超过12小时，则清除缓存
      wx.removeStorageSync('userInfo');
      wx.removeStorageSync('cacheTimestamp');
    }else{
      //从缓存中获取信息
      let userinfo = wx.getStorageSync('userinfo')
        if (userinfo && userinfo.nickName) {
          this.setData({
            userInfo: userinfo,
            hasUserInfo: true,
          })
        }
      }
  },
})